#!/usr/bin/env python

import gtk
import gtk.glade
import pango

import os
import getpass

import uspCoreUtils
import utils.GConfigurator as uspGConfig

from xdg.DesktopEntry import DesktopEntry

class UserView(object):
	def __init__(self, widgetTree, mainWindow, mainEventWindow):
		userImage = widgetTree.get_widget("UserImage1")
		homeFolder = os.path.expanduser('~')
		imageFile = os.path.join(homeFolder, '.face')
		if imageFile:
			userImage.set_from_file(imageFile)
		
		usernameLabel = widgetTree.get_widget("UsernameLabel1")
		usernameLabel.set_text(getpass.getuser())
		#usernameLabel.set_single_line_mode(True)
		usernameLabel.set_width_chars(10)
		usernameLabel.set_ellipsize(pango.ELLIPSIZE_END)
		itemStyle = pango.AttrList()
		itemStyle.insert(pango.AttrWeight(pango.WEIGHT_BOLD, 0, -1))
		itemStyle.insert(pango.AttrSize(14*1000,0,-1))
		usernameLabel.set_attributes(itemStyle)
		usernameLabel.set_alignment(0.5, 0.5)

class Configuration(uspCoreUtils.Singleton):
	def __init__(self):
		uspCoreUtils.Singleton.__init__(self)
		
class UspPlugin(object):
	
	gladeFilename = "uspUser.glade"
	
	def __init__(self):
		self.gladefile = os.path.join(os.path.dirname(__file__) , self.gladeFilename)
		
		if not os.path.exists(self.gladefile):
			raise Exception("Glade file '"+ self.gladeFilename +"' does not exist.")
			
		self.widgetTree = gtk.glade.XML(self.gladefile, "MainWindow")
		self.window = self.widgetTree.get_widget("MainWindow")
		self.content_holder = self.widgetTree.get_widget("MainEventBox")
		
		self.gClient = uspGConfig.GConfigurator('/apps/usp2/plugins/user')
		Configuration().gClient = self.gClient
		
		self.userView = UserView(self.widgetTree, self.window, self.content_holder)
	
	def pluginName(self):
		return "User"
	
	def mainWidget(self):
		return self.content_holder
	
	def pluginIcon(self):
		return 'login-photo'
	
